# -*- coding: utf-8 -*-
import sys
import scrapy
import json
import time
import re
import datetime
from lxml import etree
from weibo_scrapy.items import MblogItem, UserItem
from weibo_scrapy.settings import LIMIT_DATE,COOKIES_ENABLED,COOKIES
from weibo_scrapy.login import login
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
}

class CwbSpider(scrapy.Spider):
    name = 'cwb_1'
    allowed_domains = ['weibo.cn']

    def start_requests(self):
        mid = '3424883176420210'
        bid = 'yark9qWbM'
        uid = 1648007681
        page = 1
        url = 'https://weibo.cn/comment/{bid}?uid={uid}&rl=0&page={page}'.format(bid=bid,uid=uid,page=page)

        yield scrapy.Request(url=url,headers=HEADERS, cookies=COOKIES,meta={'root_mid':mid,'root_uid':uid,'url':url},callback=self.parse_item)

    def parse_item(self, response):
        print(response.url)
        if 'login' in response.url:
            COOKIES = login()
            url = response.meta['url']
            return scrapy.Request(url=url, cookies=COOKIES,callback=self.parse_item,meta={'url':url})
        root_mid = response.meta['root_mid']
        root_uid = response.meta['root_uid']
        mblog_list = response.xpath('//div[@class="c" and starts-with(@id,"C_")]')
        for mblog in mblog_list:
            item = MblogItem()
            item['mid'] = mblog.xpath('./@id').extract_first().lstrip('C_')
            item['root_mid'] = root_mid
            item['root_uid'] = root_uid
            item['uid'] = mblog.xpath('./a[1]/@href').extract_first().replace('/u/','')
            item['screen_name'] = mblog.xpath('./a[1]/text()').extract_first()
            item['text'] = ''.join(mblog.xpath('.//span[@class="ctt"]//text() | .//span[@class="ctt"]/img/@alt').extract())
            item['attitudes_count'] =int( re.findall('[(\d+)]',mblog.xpath('.//a[contains(@href,"attitude") and contains(@href,"uid")]/text()').extract_first())[0])
            ct = mblog.xpath('.//span[@class="ct"]/text()')
            if ct:
                date_and_source = ct.extract_first().split('\xa0')
                if len(date_and_source) == 2:
                    date_str = date_and_source[0]
                    item['source'] = date_and_source[1].replace('来自','').strip()
                elif len(date_and_source) == 1:
                    date_str = date_and_source[0]
                    item['source'] = ""
            item['created_at'],item['timestamp'] = trans_date_ts(date_str)
            yield item
        next_page = 'https://weibo.cn'+response.xpath('//a[text()="下页"]/@href').extract_first()
        yield scrapy.Request(url=next_page,callback=self.parse_item,meta={'url':next_page,'root_mid':root_mid,'root_uid':root_uid})
def trans_date_ts(date):
    date = date.replace('年','-').replace('月','-').replace('日','')
    # 转成时间戳
    if '分钟前' in date:
        now_ts = time.time()
        minute = int(date.rstrip('分钟前'))
        ts = now_ts - minute * 60
        date = ts2date(ts)
    elif '小时前' in date:
        now_ts = time.time()
        hour = int(date.rstrip('小时前'))
        ts = now_ts - hour * 3600
        date = ts2date(ts)
    elif '昨天' in date:
        now_ts = time.time()
        new_time = date.lstrip('昨天')
        date = get_before_date(1) + new_time + ":00"
    elif '今天' in date:
        now_ts = time.time()
        new_time = date.lstrip('今天')
        date = get_before_date(0) + new_time + ":00"
    elif date.count('-') == 1:
        date = time.strftime("%Y-", time.localtime()) + date+ ":00"
    elif '刚刚' in date:
        date = ts2date(time.time())
    elif '+0800' in date:
        format_time = time.strptime(date, '%a %b %d %H:%M:%S +0800 %Y')
        date = time.strftime('%Y-%m-%d %H:%M:%S', format_time)
    return date[:10], date2ts(date)
def date2ts(date):
    try:
        return int(time.mktime(time.strptime(date, '%Y-%m-%d')))
    except:
        return int(time.mktime(time.strptime(date, '%Y-%m-%d %H:%M:%S')))


def ts2date(ts):
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ts))


def get_before_date(date_cha):
    # date_cha为当天日期的前几天,返回前几天的日期
    # 如当天2018-7-7,date_cha=1,则返回2018-7-6
    today = datetime.date.today()
    oneday = datetime.timedelta(days=date_cha)
    newday = today - oneday
    return str(newday)


# if __name__ == '__main__':
#     print trans_date_ts('刚刚')
